//定制请求的实例
import { ElMessage } from 'element-plus'
import axios from "axios";
import config from "@/config/index.js";
const service= axios.create({
    baseURL:config.baseApi,
});

service.interceptors.request.use(

)

service.interceptors.response.use(
    (result)=>{
        const {code,data,message} = result.data;
        if (code===200){
            return data;
        }
        ElMessage.error( message ||'服务异常')
        return Promise.reject(result.data);
    }
)

function request(options){
    options.method = options.method || 'get' ;
    //增加一个关于get请求参数的调整
    if (options.method.toLowerCase() === "get"){
        options.params = options.data;
    }

    let isMock = config.mock;
    if (typeof options.mock !== "undefined"){
        isMock = options.mock;
    }
    //针对环境做一个处理
    if (config.env === "prod"){
        service.defaults.baseURL = config.baseApi;
    }else {
        service.defaults.baseURL = isMock ? config.mockApi : config.baseApi;
    }

    return service(options);
}

export default request ;